package cn.com.xy.sms.sdk.db.entity.pubinfo;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.db.entity.CenterNumLocationInfo;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.StringUtils;
/**
 * 短信中心归属地管理
 * @author Administrator
 *
 */
public class CNumLocationInfoManager {
    /* SDK-450 huangzhiqiang 20160622 start */
    private static final Map<String, CenterNumLocationInfo> CENTER_NUMBER_LOACTION_INFO_CACHE = new HashMap<String, CenterNumLocationInfo>();

    public static void addCenterNumberLoactionInfoCache(String centerNum, CenterNumLocationInfo centerNumLocationInfo) {
        CENTER_NUMBER_LOACTION_INFO_CACHE.put(centerNum, centerNumLocationInfo);
    }

    public static CenterNumLocationInfo getCenterNumberLoactionInfoCache(String centerNum) {
        return CENTER_NUMBER_LOACTION_INFO_CACHE.get(centerNum);
    }

    /**
     * 保存短信中心归属地
     * @param info
     */
    public static boolean saveCNumLocationInfo(CenterNumLocationInfo info) {
        try {
            ContentValues values = setContentValues(info);
            long flag = DBManager.update(CenterNumLocationInfo.TABLE_NAME, values, "cnum = ?",
                    new String[] { String.valueOf(info.cNum) });
            if (flag < 1) {
                flag = DBManager.insert(CenterNumLocationInfo.TABLE_NAME, values);
            }
            return flag > 0;
        } catch (Throwable e) {
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "saveCNumLocationInfo: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
        return false;
    }
    /* SDK-450 huangzhiqiang 20160622 end */
    
    static ContentValues setContentValues(CenterNumLocationInfo info){
         ContentValues values = new ContentValues();
         values.put("cnum", info.cNum);
         if(!StringUtils.isNull(info.areaCode)){
          values.put("areaCode", info.areaCode);
         }
         if(!StringUtils.isNull(info.city)){
          values.put("city", info.city);
         }
         if(!StringUtils.isNull(info.operator)){
          values.put("operator", info.operator);
         }
         values.put("checkTime", info.checkTime);
         return values;
    }
    /**
     * 依据短信中心查询,归属地
     * @param cnum
     * @return
     */
    public static CenterNumLocationInfo queryCenterNumLocationInfo(String cnum){

          XyCursor cur = null;
       
          CenterNumLocationInfo centerNumLocationInfo = null;

    try {
          cnum = StringUtils.getSubString(cnum);
          cur = DBManager.query(CenterNumLocationInfo.TABLE_NAME, new String[] {
              "cnum","areaCode","city","operator","checkTime"}, 
            "cnum = ? ", new String[] {cnum+""});
          if (cur != null && cur.getCount() > 0) {
             // int cnumIndex = cur.getColumnIndex("cnum");   
              int areaCodeIndex = cur.getColumnIndex("areaCode");  
              int cityIndex = cur.getColumnIndex("city");   
              int operatorIndex = cur.getColumnIndex("operator");   
              int checkTimeIndex = cur.getColumnIndex("checkTime");   
                          
              while (cur.moveToNext()) {
                  centerNumLocationInfo = new CenterNumLocationInfo();
                  centerNumLocationInfo.cNum =cnum;
                  centerNumLocationInfo.areaCode =cur.getString(areaCodeIndex);
                  centerNumLocationInfo.city=cur.getString(cityIndex);
                  centerNumLocationInfo.operator=cur.getString(operatorIndex);
                  centerNumLocationInfo.checkTime=cur.getLong(checkTimeIndex);
              }
          }
      } catch (Throwable e) {
//          e.printStackTrace();
          /*SDK-550 hewengao 20160719 start*/
          LogManager.w(Constant.TAG, "queryCenterNumLocationInfo: ", e);
          /*SDK-550 hewengao 20160719 start*/
      } finally {
          XyCursor.closeCursor(cur, true);
      }       
      return centerNumLocationInfo;
    
    }
     
     
}
